package com.hsteach.service.buildingApply;

import com.google.common.collect.Lists;
import com.hsteach.common.utils.bean.BeanUtils;
import com.hsteach.dao.domain.buildingApply.BuildingApply;
import com.hsteach.enums.RepeatTypeEnum;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class DayApplyHandlerImpl implements RepeatApplyHandler{
    @Override
    public RepeatTypeEnum getType() {
        return RepeatTypeEnum.DAY;
    }

    @Override
    public List<BuildingApply> handle(BuildingApply apply, Date start, Date end) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(apply.getEndTime());
        List<BuildingApply> ret = Lists.newArrayList();
        while (calendar.getTime().compareTo(end) <= 0) {
            BuildingApply one = new BuildingApply();
            BeanUtils.copyProperties(apply, one);
            one.setBeginTime(calendar.getTime());
            one.setBeginDate(calendar.getTime());
            one.setEndTime(endCalendar.getTime());
            one.setId(null);
            ret.add(one);
            endCalendar.add(Calendar.DATE, 1);
            calendar.add(Calendar.DATE, 1);
        }
        return ret;
    }
}
